﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 22/04/2009
// Time: 21:37
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmRegisterBillMvt.
	/// </summary>
	public partial class frmRegisterBillMvt : common.RegisterTemplate
	{
		BillMvt billMvt= new BillMvt();
		BillMvts list = new BillMvts();
		
		Banks listBanks = new Banks();
		general.Types listTypes = new csharp.rad.general.Types();
		
		public frmRegisterBillMvt()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		#region overrided methods
			protected override void newEvent()
			{
				billMvt.clear();
				txtName.Focus();
				chkActive.Checked = true;
			}
            
			/// <summary>
			/// Set data from edits to class
			/// </summary>
			protected override void setClass()
			{
				billMvt.name = txtName.Text;
				billMvt.agencyName= txtAgencyName.Text;
				billMvt.agencyNumber= txtAgencyNumber.Text;
				billMvt.agencyDigit= txtAgencyDV.Text;
				billMvt.billNumber= txtBillNumber.Text;
				billMvt.billDigit= txtBillDV.Text;
				billMvt.active = chkActive.Checked.ToString();
				billMvt.balanceDate= Convert.ToDateTime(dtpBalanceDate.Text).ToString("yyyyy/MM/dd");
				billMvt.balanceValue= txtBalanceValue.Text;
				billMvt.bankId = (cmbBank.SelectedItem as Bank).id;
				billMvt.typeId = (cmbType.SelectedItem as general.TType).id;
				billMvt.companyId = Information.companyId;
			}
			
			/// <summary>
			/// Set data from class to edits
			/// </summary>
			protected override void setEdits()
			{
				billMvt.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				
				if (billMvt.get())
				{
					txtName.Text = billMvt.name;
					txtAgencyName.Text = billMvt.agencyName;
					txtAgencyNumber.Text = billMvt.agencyNumber;
					txtAgencyDV.Text = billMvt.agencyDigit;
					txtBillNumber.Text = billMvt.billNumber;
					txtBillDV.Text = billMvt.billDigit;
					chkActive.Checked = Convert.ToBoolean(billMvt.active);
					dtpBalanceDate.Text = Convert.ToDateTime(billMvt.balanceDate).ToString("dd/MM/yyyy");
					txtBalanceValue.Text = billMvt.balanceValue;
					Functions.setSelectIndexComboBank(cmbBank, billMvt.bankId);
					setSelectedIndexType(billMvt.typeId);
				}
			}
			
			/// <summary>
			/// Fill the DataGridView
			/// </summary>
			protected override void fillDataGridView()
			{
				int tam;
				list.fillList();
				
				tam = list.count();
				dgvMain.Rows.Clear();
				for (int i=0; i < tam; i++)
				{
					dgvMain.Rows.Add(new String[] {list.get(i).id,
					                 	list.get(i).name});
				}				
			}
			
			/// <summary>
			/// Find the object of search
			/// </summary>
			protected override void find()
			{
				common.Util.searchInGrid(dgvMain,"name", txtSearch.Text);			
			}		
			
		   /// <summary>
		   /// Save the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
		   protected override bool save()
		   {
		   	return billMvt.save();
		   }
		   
		   /// <summary>
		   /// Update the register in the database
		   /// </summary>
		   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
		   protected override bool update()
		   {   
		   	return billMvt.update();
		   }
		   
		   /// <summary>
		   /// Delete the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
		   protected override bool delete()
		   {
            billMvt.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
		   	return billMvt.delete();
		   }
		   
		   /// <summary>
		   /// Method call in initialization
		   /// </summary>
		   protected override void init()
		   {
		   	DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
		   	DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
		   	
		   	id.Name = "id";
		   	id.Visible = false;
		   	
		   	name.Name = "name";
		   	name.HeaderText = "Nome";
		   	name.Width = 300;            
		   	
		   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id, name});

		   	listBanks.fillList();
		   	Functions.fillComboBank(listBanks, cmbBank, "id", "name");
		   	
		   	listTypes.fillList("bill_mvt");
		   	fillComboType();
		   }
		#endregion	
		
		private void fillComboType()
		{
			for(int i=0; i < listTypes.count(); i++)
				cmbType.Items.Add(listTypes.get(i));
			cmbType.ValueMember = "id";
			cmbType.DisplayMember = "name";
		}
		
		private void setSelectedIndexType(string typeId)
		{
			for(int i=0; i < listTypes.count(); i++)
			{
				if ( (cmbType.Items[i] as general.TType).id.Equals(typeId))
				{
					cmbType.SelectedIndex = i;
					break;
				}
			}
		}
	}
}
